---
title: "tiktok_usage_effect"
output: html_document
editor_options: 
  chunk_output_type: console
---

```{r}
rm(list = ls())
library(data.table); library(tidyverse); library(psych); library(fixest); library(marginaleffects);
```


```{r}
d0 <- fread("../data/clean/data-long.csv")
glimpse(d0)
d0 <- d0[veracity != "none"]
# d0 <- d0[screenacc == 1.0]
unique(d0$responseid)
summary(d0)

```

```{r recode}
d0[, table(condition)]
d0[, condition := factor(condition)]

d0[, table(veracity)]
d0[, veracityc := ifelse(veracity == "false", -0.5, 0.5)]
d0[, table(veracityc)]

d0[, responseid := factor(responseid)]
glimpse(d0)

d0[, condition := factor(condition, levels = c("misinfo-only", "correction-only", "debunk"), 
                         labels = c("Misinfo", "Correction", "Debunk"))]
d0[, condition := relevel(condition, "Misinfo")]

d0[, veracitybin := ifelse(veracity == "false", 0, 1)]

d0$tiktokuseC <- d0$tiktokuse - mean(d0$tiktokuse, na.rm = TRUE)
mean(d0$tiktokuseC, na.rm = T)


d0[, unique(tiktokuse)]
d5 <- d0[!is.na(tiktokuse)]

d5$belief <- -1
d5[topic == "rust", belief := rust]
d5[topic == "ive", belief := ivermectin]
d5[topic == "asp", belief := aspartame]
d5[topic == "asymp", belief := asymptomatic]
d5[topic == "herd", belief := herd]
d5[topic == "brain", belief := brain]
d5[belief == -1]
```



```{r}
# video credibility
m <- feols(videorating ~ veracitybin * condition * tiktokuseC, d5, cluster = "responseid")
m
mfx5_feol <- marginaleffects(m, by = c("veracitybin", "condition"))
tidy(mfx5_feol) |> mutate_if(is.numeric, round, 3)

# false statement belief
dbelief <- select(d5, responseid, topic, condition, belief, tiktokuseC, tiktokuse) |> distinct()
m_belief <- lm(belief ~ condition*tiktokuseC, dbelief)
summary(m_belief)

replace_names <- as_labeller(
     c(`Misinfo` = "Misinformation-only", `Correction` = "Correction-only",`Debunk` = "Correction after misinformation (debunking)"))
p2 <- ggplot(dbelief, aes(tiktokuse, belief, col = condition)) +
    facet_wrap(~condition, labeller = replace_names) +
    geom_smooth(method = "lm", alpha = 0.3) + 
    geom_jitter(size = 0.8, alpha = 0.3) + xlab("TikTok usage") + ylab("Belief in false claim") + 
    theme(legend.position = "none") + scale_x_continuous(sec.axis = sec_axis(~ . , name = "Condition", breaks = NULL, labels = NULL))
p2    
ggsave("../figures/tiktokuse_vs_belief.png", dpi = 300, bg = 'white', width = 8, height = 5)
```

```{r}

```

